package cn.tedu.web.fqmz;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = "/fqmz/*")
public class CoreFilter implements Filter {


    //销毁函数，tomcat停止时触发
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response= (HttpServletResponse) resp;

        String uri = request.getRequestURI();
        if ("/fqmz/login.jsp".equals(uri) || uri.startsWith("/fqmz/assets")) {
            chain.doFilter(req, resp);
            return;
        }

        HttpSession session = request.getSession();
        Object uid = session.getAttribute("UID");
        if (uid == null) {
            response.sendRedirect("/fqmz/login.jsp");
            return;
        }

        //如果调用doFilter则说明通行，否则不通行
        chain.doFilter(req, resp);
    }


    //初始化函数，在启动时出发

    public void init(FilterConfig config) throws ServletException {

    }


}
